<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Backup_handle extends MY_Controller
{
    function get_list()
    {
        $path = $this->config->item( 'db_backup_path' );
        $infos = $info = $names = $dates = array ();
        foreach ( glob( $path . '*.sql' ) as $filename )
        {
            if (! is_file( $filename ))
            {
                continue;
            }
            $size = filesize( $filename );
            if ($size < 1048576)
            {
                $info['size'] = round( $size / 1024, 2 ) . "K";
            }
            else
            {
                $info['size'] = round( $size / (1024 * 1024), 2 ) . "M";
            }
            $info['date'] = date( 'Y-m-d H:i:s', filectime( $filename ) );
            $dates[] = $info['date'];
            if (DIRECTORY_SEPARATOR == '\\')
            {
                $filename = iconv( 'GB2312', 'UTF-8', $filename );
            }
            $info['name'] = basename( $filename );
            $names[] = $info['name'];
            $infos[] = $info;
        }
        array_multisort( $dates, SORT_DESC, $names, SORT_DESC, $infos );
        $this->rs->ok( 'list', $infos );
    }
    function create()
    {
        $name = $this->input->post( 'name' );
        if (empty( $name ) || $name == '备份名称')
        {
            $name = date( 'Y年m月d日H时i分s秒_数据库备份', $_SERVER['REQUEST_TIME'] );
        }
        if (! preg_match( '/^[0-9a-zA-Z_\x{4e00}-\x{9fa5}]+$/iu', $name ))
        {
            $this->rs->err( '名称包含不安全字符！' );
        }
        if (! is_file( $file = APPPATH . 'include/MysqlBackup.class.php' ))
        {
            $this->rs->err( '缺少类文件！' );
        }
        include ($file);
        $path = $this->config->item( 'db_backup_path' );
        $backup = new MysqlBackup( $path );
        if ($backup->create( $name . '.sql' ))
        {
            $this->rs->ok();
        }
        else
        {
            $this->rs->err( $backup->get_error() );
        }
    }
    function restore()
    {
        $token = $this->input->post( 'token' );
        if (! $this->check_csrf( $token ))
        {
            $this->rs->err( '非法请求！' );
        }
        $name = $this->input->post( 'name' );
        if (empty( $name ))
        {
            $this->rs->err( '名称不能为空！' );
        }
        if (! preg_match( '/^[0-9a-zA-Z_\x{4e00}-\x{9fa5}]+\.sql$/iu', $name ))
        {
            $this->rs->err( '名称包含不安全字符！' );
        }
        if (! is_file( $file = APPPATH . 'include/MysqlBackup.class.php' ))
        {
            $this->rs->err( '缺少类文件！' );
        }
        include ($file);
        $path = $this->config->item( 'db_backup_path' );
        $backup = new MysqlBackup( $path );
        if ($backup->restore( $name ))
        {
            $this->rs->ok();
        }
        else
        {
            $this->rs->err( $backup->get_error() );
        }
    }
    function delete()
    {
        $token = $this->input->post( 'token' );
        if (! $this->check_csrf( $token ))
        {
            $this->rs->err( '非法请求！' );
        }
        $name = $this->input->post( 'name' );
        if (empty( $name ))
        {
            $this->rs->err( '名称不能为空！' );
        }
        if (! preg_match( '/^[0-9a-zA-Z_\x{4e00}-\x{9fa5}]+\.sql$/iu', $name ))
        {
            $this->rs->err( '名称包含不安全字符！' );
        }
        $path = $this->config->item( 'db_backup_path' );
        $file = $path . $name;
        if (DIRECTORY_SEPARATOR == '\\')
        {
            $file = iconv( 'UTF-8', 'GB2312', $file );
        }
        if (! is_file( $file ))
        {
            $this->rs->err( '文件不存在！' );
        }
        if (@unlink( $file ))
        {
            $this->rs->ok();
        }
        else
        {
            $this->rs->err( '删除失败！' . $file );
        }
    }
    function download()
    {
        $name = $this->input->get( 'name' );
        if (empty( $name ))
        {
            $this->rs->err( '名称不能为空！' );
        }
        if (! preg_match( '/^[0-9a-zA-Z_\x{4e00}-\x{9fa5}]+\.sql$/iu', $name ))
        {
            $this->rs->err( '名称包含不安全字符！' );
        }
        $path = $this->config->item( 'db_backup_path' );
        $file = $path . $name;
        if (DIRECTORY_SEPARATOR == '\\')
        {
            $file = iconv( 'UTF-8', 'GB2312', $file );
        }
        if (! is_file( $file ))
        {
            $this->rs->err( '文件不存在！' );
        }
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match( "/MSIE/", $user_agent ))
        {
            header( 'content-disposition:attachment; filename="' . rawurlencode( $name ) . '"' );
        }
        else if (preg_match( "/Firefox/", $user_agent ))
        {
            header( 'content-disposition:attachment; filename*="utf8\'\'' . $name . '"' );
        }
        else
        {
            header( 'content-disposition:attachment; filename="' . $name . '"' );
        }
        @readfile( $file );
    }
}